package com.wsjzzcbq.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsjzzcbq.bean.Order;
import com.wsjzzcbq.feign.AccountFeign;
import com.wsjzzcbq.mapper.OrderMapper;
import com.wsjzzcbq.service.OrderService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;

/**
 * OrderServiceImpl
 *
 * @author wsjz
 * @date 2022/07/07
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private AccountFeign accountFeign;

    @GlobalTransactional
    @Override
    public void create(String userId, int money, boolean rollback) {
        String xid = RootContext.getXID();
        System.out.println(xid);
        String orderCode = UUID.randomUUID().toString();

        Order order = new Order();
        order.setCode(orderCode);
        order.setCount(1);
        order.setUserId(userId);
        order.setMoney(money);

        this.save(order);

        accountFeign.debit(userId, money);

        if (rollback) {
            int a = 1/0;
        }

    }
}
